﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Windows.Forms;

namespace QLTVNEW
{
    public partial class frmTraSach : DevComponents.DotNetBar.Office2007Form
    {
        public frmTraSach()
        {
            InitializeComponent();
        }

        QLTVDataContext db = new QLTVDataContext();
        PHIEUMUONTRASACH tbphieu = new PHIEUMUONTRASACH();
        SACH tbsach = new SACH();
        THELOAI tbtl = new THELOAI();
        DOCGIA tbdg = new DOCGIA();
        #region 'LT'

        //void LoadDau()
        //{
        //    txtTenSach.Text = GVW.Rows[0].Cells[0].Value.ToString().Trim();
        //    txtTheLoai.Text = GVW.Rows[0].Cells[1].Value.ToString().Trim();
        //    txtMaSach.Text = GVW.Rows[0].Cells[2].Value.ToString().Trim();
        //    txtMaDG.Text = GVW.Rows[0].Cells[3].Value.ToString().Trim();
        //    txtTen.Text = GVW.Rows[0].Cells[4].Value.ToString().Trim();
        //    mktMuonSach.Text = GVW.Rows[0].Cells[5].Value.ToString().Trim();
        //    mktTraSach.Text = GVW.Rows[0].Cells[6].Value.ToString().Trim();
          
        //}

        //void LoadGridView(DataGridView gv)
        //{
        //    var query = from tphieu in db.PHIEUMUONTRASACHes
        //                join tbdg in db.DOCGIAs on tphieu.MaDocGia equals tbdg.MaDocGia
        //                join tbsach in db.SACHes on tphieu.MaSach equals tbsach.MaSach
        //                where tphieu.LoaiMuonTra == "Mượn"
        //                select new
        //                {
        //                    tbsach.TenSach,
        //                    tbsach.THELOAI.TenTheLoai,
        //                    tbsach.MaSach,
        //                    tbdg.MaDocGia,
        //                    tbdg.HoTen,
        //                    tphieu.NgayMuonSach,
        //                    tphieu.NgayTraSach,

        //                };
        //    gv.DataSource = query;

        //}


        #endregion

        private void frmTraSach_Load(object sender, EventArgs e)
        {
            tbdg.LoadCBMuonTra(cbxMaDG);
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            foreach (Control ctr in groupBox1.Controls)
            {
                if (ctr is MaskedTextBox || ctr is TextBox)
                {
                    ctr.Text = "";
                    labelX2.Focus();
                }
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.AutoValidate = AutoValidate.Disable;
            this.Close();
        }

        DateTime GetNgayThang(string s)
        {
            s = s.Trim();
            string ngay = s.Substring(0, s.IndexOf('/'));
            string thang = s.Substring(s.IndexOf('/') + 1, s.LastIndexOf('/') - ngay.Length - 1);
            string nam = s.Substring(s.LastIndexOf('/') + 1);
            DateTime dt = new DateTime(int.Parse(nam), int.Parse(thang), int.Parse(ngay));
            return dt;
        }

        DateTime date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

        private void btnTra_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Trả Sách Không", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {

                try
                {
                   if (listBox1.Items.Count==0)
                   {
                       MessageBox.Show("Bạn Không Có Sách Để Trả.","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                   } 
                   else
                   {
                       if (cbxMaDG.Text != "")
                       {
                           string err = "";
                           TimeSpan songay = date - GetNgayThang(mktTraSach.Text);
                           int ngay = Convert.ToInt32(songay.TotalDays);
                           if (ngay > 0)
                           {
                             
                               int sosach = (from tbp in db.PHIEUMUONTRASACHes
                                             where tbp.MaDocGia == int.Parse(cbxMaDG.Text)
                                             && tbp.LoaiMuonTra == "Mượn"
                                             select tbp.MaSach).Count();
                           
                               MessageBox.Show("Bạn Trả Sách Trễ Hạn." + Environment.NewLine + "Nộp Phạt " + (sosach * ngay * 1000).ToString() + "đ.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                               var query = (from tbp in db.PHIEUMUONTRASACHes
                                            where tbp.LoaiMuonTra == "Mượn" && tbp.MaDocGia == int.Parse(cbxMaDG.Text)
                                            select new
                                            {
                                                tbp.MaDocGia,
                                                tbp.MaSach,
                                                tbp.NgayMuonSach
                                            });
                               foreach (var item in query)
                               {

                                   db.uspCRUDTraSachTre(item.MaDocGia, item.MaSach, item.NgayMuonSach, ref err);
                                   db.SubmitChanges();
                               }
                               MessageBox.Show(err.ToString(), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                           }
                           else
                           {
                               var query = (from tbp in db.PHIEUMUONTRASACHes
                                            where tbp.LoaiMuonTra == "Mượn" && tbp.MaDocGia == int.Parse(cbxMaDG.Text)
                                            select new
                                            {
                                                tbp.MaDocGia,
                                                tbp.MaSach,
                                                tbp.NgayMuonSach
                                            });
                               foreach (var item in query)
                               {

                                   db.uspTraSach(item.MaDocGia, item.MaSach, item.NgayMuonSach, ref err);
                                   db.SubmitChanges();
                               }
                               MessageBox.Show(err.ToString(), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                           }

                       }
                       else
                       {
                           MessageBox.Show("Bạn Chưa Nhập Đầy Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                       }
                   }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message,"Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
            }
        }

    
       

        private void buttonX1_Click(object sender, EventArgs e)
        {
            frmLocSanhTra frm = new frmLocSanhTra();
            frm.ShowDialog();
        }

        private void cbxMaDG_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            txtTen.Text = tbdg.GetTenDocGia(int.Parse(cbxMaDG.Text));
            var query = (from tbp in db.PHIEUMUONTRASACHes
                         join tbs in db.SACHes on tbp.MaSach equals tbs.MaSach
                         where tbp.MaDocGia == int.Parse(cbxMaDG.Text) && tbp.LoaiMuonTra == "Mượn"
                         select new
                         {
                             tbs.TenSach
                         });
            foreach (var item in query)
            {
                listBox1.Items.Add(item.TenSach);
            }
          
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           txtMaSach.Text= tbsach.GetMaSach(listBox1.SelectedItem.ToString()).ToString();
           txtTheLoai.Text = tbsach.GetTheLoaiTheoMa(int.Parse(txtMaSach.Text));
           var query = (from tbp in db.PHIEUMUONTRASACHes
                        where tbp.MaSach == int.Parse(txtMaSach.Text) && tbp.LoaiMuonTra == "Mượn"
                        select new
                        {
                            tbp.NgayMuonSach,
                            tbp.NgayTraSach
                        });
           foreach (var item in query)
           {
               mktMuonSach.Text = item.NgayMuonSach;
               mktTraSach.Text = item.NgayTraSach;
           }
        }

       
    }
}
